package com.mcptt.config.body.info.mcdata.binary;

import com.cloudptt.api.product.log.Log;

import java.nio.charset.StandardCharsets;

public class PayloadBody extends BinaryBody{

    private static byte SIZE_PAYLOAD = 0x01;

    private int type = Type_Text;
    private String content = null;

    public PayloadBody()
    {

    }
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
    public String toString()
    {
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("type = ").append(type).append(",");
        strBuf.append("content = ").append(content).append(".");

        return strBuf.toString();
    }
    public static String toSdsBodyStr(String content)
    {
        Log.debug(PayloadBody.class,"toDataArrStr content =  " + content);
        byte[] srcArray = content.getBytes(StandardCharsets.UTF_8);
        Log.debug(PayloadBody.class,"toBodyStr srcArray =  " ,srcArray);
        int len = srcArray.length;

        byte[] allArray = new byte[1+1+1+2+1+len];
        allArray[0] = DATA_PAYLOAD;
        allArray[1] = SIZE_PAYLOAD;
        allArray[2] = IE_PAYLOAD;
        byte[] lenArr = intToByteArray(len+1);
        Log.debug(PayloadBody.class,"toBodyStr lenArr =  " ,lenArr);
        allArray[3] = lenArr[0];
        allArray[4] = lenArr[1];
        allArray[5] = Type_Text;
        System.arraycopy(srcArray, 0, allArray, 6, len);
        Log.debug(PayloadBody.class,"toBodyStr allArray =  ",allArray);

        return byteArrayToHexStr(allArray);
    }

    public static PayloadBody parsePayload(String hexString) throws Exception
    {
        PayloadBody body = new PayloadBody();
        byte[] contentArr = hexStringToByteArray(hexString);//content.getBytes(StandardCharsets.UTF_8);
        Log.debug(PayloadBody.class,"parsePayload contentArr.length =  " + contentArr.length + " contentArr =  ",contentArr);
        byte type = contentArr[0];
        Log.debug(PayloadBody.class,"parsePayload type =  " + String.format("%02X ", type));
        switch (type)
        {
            case DATA_PAYLOAD:
                body = parseSdsPayload(contentArr);
                break;
            default:
                break;
        }
        return body;
    }

    private static PayloadBody parseSdsPayload(byte[] contentArr)
    {
        PayloadBody body = new PayloadBody();
        byte[] lenArr = new byte[2];
        lenArr[0] = contentArr[3];
        lenArr[1] = contentArr[3 + 1];
        int startIndex = 5;
        int len = byteArrToInt(lenArr);
        Log.debug(PayloadBody.class,"parseSdsPayload len =  " + len + " contentArr.length = " + contentArr.length);
        // 检查索引和子组长度是否有效
        if (startIndex < 0 || startIndex >= contentArr.length ||
                startIndex + len > contentArr.length) {
            Log.debug(PayloadBody.class,"parseSdsPayload Invalid start index or subgroup length, cannot extract subContentArr.");
            return body;
        }

        int type = contentArr[startIndex];
        body.setType(type);
        // 提取n位byte子组
        byte[] subContentArr = new byte[len-1];
        System.arraycopy(contentArr, startIndex+1, subContentArr, 0, len-1);
        Log.debug(PayloadBody.class,"toBodyStr subContentArr =  " ,subContentArr);
        String content = new String(subContentArr, StandardCharsets.UTF_8);
        Log.debug(PayloadBody.class,"toBodyStr content =  " + content);
        body.setContent(content);
        return body;
    }

}
